package com.tlgen.service;

import com.tlgen.mapper.EnlistMapper;
import com.tlgen.model.Enlist;
import com.tlgen.model.RespPageBean;
import com.tlgen.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author: tlgen
 * @Discription:
 */
@Service
public class EnlistService {

    @Autowired
    EnlistMapper enlistMapper;

    public Integer addEnlists(List<Enlist> list) {
        return enlistMapper.addEnlists(list);
    }

    public RespPageBean getEnlistByPage(Integer page, Integer size, Enlist enlist) {
        if (page != null && size != null) {
            page = (page-1) * size;
        }
        List<Enlist> data = enlistMapper.getEnlistByPage(page, size, enlist);
        Long total = enlistMapper.getTotal(enlist);
        RespPageBean bean = new RespPageBean();
        bean.setData(data);
        bean.setTotal(total);
        return bean;
    }

    public Integer addEnlist(Enlist enlist) {
        enlist.setCreateDate(new Date());
        enlist.setUpdateDate(new Date());
        enlist.setEnabled(true);
        return enlistMapper.insertSelective(enlist);
    }

    public Integer deleteEnlistByEid(Integer id) {
        return enlistMapper.deleteByPrimaryKey(id);
    }

    public Integer updateEnlist(Enlist enlist) {
        enlist.setUpdateDate(new Date());
        //如果是学生登录报名的时候保存自己的报名姓名 移动端使用
            enlist.setEnrollStudent(SecurityContextHolder.getContext().getAuthentication().getName());
        return enlistMapper.updateByPrimaryKeySelective(enlist);
    }
}
